using System;
using System.Drawing;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace SceneEditor.UI
{
    [System.Drawing.ToolboxBitmapAttribute(typeof(System.Windows.Forms.Panel)), System.ComponentModel.DefaultProperty("HeaderText")]
    public partial class KryptonCollapseContainer : System.Windows.Forms.Panel
    {
        private Color borderColor = Color.FromArgb(101, 147, 207);
        private Color normalLightColor = Color.FromArgb(227, 239, 255);
        private Color normalDarkColor = Color.FromArgb(214, 232, 255);
        private Color rollOverLightColor = Color.White;
        private Color rollOverDarkColor = Color.FromArgb(227, 239, 255);

        private IPalette palette;
        private PaletteRedirect paletteRedirect;

        private bool mouseOver = false;

        private int headerHeight = 20;
        private string headerText;

        private bool collapsable = true;
        private bool panelOpen = true;
        private int openHeight;

        public int OpenHeight
        {
            get { return openHeight; }
            set { openHeight = value; }
        }

        public string HeaderText
        {
            get { return headerText; }
            set
            {
                headerText = value;
                this.Refresh();
            }
        }

        public bool Collapsable
        {
            get { return collapsable; }
            set
            {
                collapsable = value;
                this.Refresh();
            }
        }
        [RefreshProperties(RefreshProperties.All)]
        public bool PanelOpen
        {
            get { return panelOpen; }
            set
            {
                panelOpen = value;

                if (!panelOpen)
                    this.Collapsable = true;

                SetContentPanel();
            }
        }

        public override Rectangle DisplayRectangle
        {
            get
            {
                Padding myPadding = this.Padding;

                Rectangle myClientRect = new Rectangle(0 + myPadding.Left, headerHeight + myPadding.Top,
                    this.Width - myPadding.Right, (this.Height - (headerHeight + 1)) - myPadding.Bottom);

                return myClientRect;
            }
        }

        public KryptonCollapseContainer()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

            UpdateStyles();

            base.FontChanged += new EventHandler(this.CollapseContainer_FontChanged);
            base.Paint += new PaintEventHandler(this.CollapseContainer_Paint);
            base.SizeChanged += new EventHandler(this.CollapseContainer_SizeChanged);
            base.MouseMove += new MouseEventHandler(this.CollapseContainer_MouseMove);
            base.MouseLeave += new EventHandler(this.CollapseContainer_MouseLeave);
            base.Click += new EventHandler(this.CollapseContainer_Click);

            this.BackColor = System.Drawing.SystemColors.Control;
            headerText = this.Name.ToString();

            this.Font = new System.Drawing.Font("Segoe UI", 9F);

            if (palette != null)
                palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            KryptonManager.GlobalPaletteChanged += new EventHandler(OnGlobalPaletteChanged);

            palette = KryptonManager.CurrentGlobalPalette;
            paletteRedirect = new PaletteRedirect(palette);

            borderColor = palette.ColorTable.ButtonSelectedBorder;
            normalLightColor = palette.ColorTable.ToolStripGradientBegin;
            normalDarkColor = palette.ColorTable.ToolStripGradientEnd;
            rollOverLightColor = Color.White;
            rollOverDarkColor = palette.ColorTable.ToolStripGradientEnd;

            InitializeComponent();
        }

        private void CollapseContainer_FontChanged(object sender, System.EventArgs e)
        {
            headerHeight = (int)((Control)sender).Font.Size + 11;

            if (!panelOpen)
                this.Height = headerHeight;

            this.PerformLayout();
        }

        private void CollapseContainer_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (palette == null)
            {
                EventArgs Ev = new EventArgs();
                OnGlobalPaletteChanged(this, Ev);
            }

            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), 0, 0, Bounds.Width, Bounds.Height);

            e.Graphics.FillRectangle(Brushes.White, 0, 0, Bounds.Width, Bounds.Height);

            Color sepColor = palette.ColorTable.SeparatorDark;
            Color colorLight = palette.ColorTable.ToolStripGradientBegin;
            Color colorDark = palette.ColorTable.ToolStripGradientMiddle;

            if (mouseOver)
            {
                colorDark = colorLight;
                colorLight = Color.White;
            }

            Rectangle headerRect = new Rectangle(0, 0, Bounds.Width, headerHeight);

            LinearGradientBrush headerBrush = new LinearGradientBrush(headerRect, 
                Color.White, Color.White, LinearGradientMode.Vertical);

            headerBrush.WrapMode = WrapMode.TileFlipX;
            headerBrush.SetSigmaBellShape(0.1F);

            ColorBlend cb = new ColorBlend(3);

            cb.Positions[0] = 0;
            cb.Colors[0] = colorLight;

            cb.Positions[1] = .95f;
            cb.Colors[1] = colorDark;

            cb.Positions[2] = 1f;
            cb.Colors[2] = colorDark;

            headerBrush.InterpolationColors = cb;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.CompositingMode = CompositingMode.SourceOver;
            e.Graphics.FillRectangle(headerBrush, headerRect);

            if (collapsable)
            {
                int ButtonY = (int)(headerHeight / 2) - (imgButtons.Images[0].Height / 2);

                if (panelOpen)
                    e.Graphics.DrawImage(imgButtons.Images[0], Bounds.Width - 15, ButtonY);
                else
                    e.Graphics.DrawImage(imgButtons.Images[1], Bounds.Width - 15, ButtonY);
            }

            e.Graphics.DrawLine(new Pen(sepColor), 0, headerHeight
                - 1, Bounds.Width, headerHeight - 1);

            StringFormat textFormat = new StringFormat();

            textFormat.FormatFlags = StringFormatFlags.NoWrap;
            textFormat.Trimming = StringTrimming.EllipsisCharacter;
            textFormat.Alignment = StringAlignment.Near;
            textFormat.LineAlignment = StringAlignment.Center;

            GraphicsPath textGraphicsPath = new GraphicsPath();
            SolidBrush fontBrush = new SolidBrush(palette.ColorTable.StatusStripText);

            try
            {
                Rectangle fontBounds = new Rectangle(10, 2, Bounds.Width, headerHeight - 2);

                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                e.Graphics.DrawString(headerText, Font, fontBrush, fontBounds, textFormat);
            }

            catch (Exception ex) { MessageBox.Show(ex.Message); }

            textGraphicsPath.Dispose();
            textGraphicsPath = null;

            textFormat.Dispose();
            textFormat = null;

            e.Graphics.DrawLine(new Pen(palette.ColorTable.SeparatorDark), 
                0, Bounds.Height - 1, Bounds.Width, Bounds.Height - 1);
        }

        [RefreshProperties(RefreshProperties.All)]
        private void CollapseContainer_SizeChanged(object sender, System.EventArgs e)
        {
            this.Refresh();
        }

        private void CollapseContainer_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (!collapsable)
                return;

            if (e.Y > 0 & e.Y < 21)
            {
                if (!mouseOver)
                {
                    mouseOver = true;
                    this.Cursor = Cursors.Hand;
                    this.Refresh();
                }
            }
            else
            {
                if (mouseOver)
                {
                    mouseOver = false;
                    this.Cursor = Cursors.Default;
                    this.Refresh();
                }
            }
        }

        private void CollapseContainer_MouseLeave(object sender, System.EventArgs e)
        {
            if (collapsable)
            {
                mouseOver = false;

                if (this.Cursor == Cursors.Hand)
                    this.Cursor = Cursors.Default;

                this.Refresh();
            }
        }

        private void CollapseContainer_Click(object sender, System.EventArgs e)
        {
            if (collapsable)
            {
                panelOpen = (panelOpen == false);
                SetContentPanel();
            }
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (palette != null)
                palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

            palette = KryptonManager.CurrentGlobalPalette;
            paletteRedirect.Target = palette;

            if (palette != null)
            {
                palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(OnPalettePaint);

                //set colors
                borderColor = palette.ColorTable.ButtonSelectedBorder;
                normalLightColor = palette.ColorTable.ToolStripGradientBegin;
                normalDarkColor = palette.ColorTable.ToolStripGradientEnd;
                rollOverLightColor = Color.White;
                rollOverDarkColor = palette.ColorTable.ToolStripGradientEnd;
            }

            Invalidate();
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            Invalidate();
        }

        private void SetContentPanel()
        {
            if (panelOpen)
            {
                foreach (Control control in this.Controls)
                    control.Visible = true;

                if (this.Dock != DockStyle.Fill)
                    this.Height = this.OpenHeight;
            }
            else
            {
                foreach (Control control in this.Controls)
                    control.Visible = false;

                if (this.Dock != DockStyle.Fill)
                    this.Height = headerHeight;
            }

            this.Refresh();
        }

        #region Hidden Properties
        [Browsable(false)]
        public override bool  AutoSize
        {
            get { return base.AutoSize; }
            set { base.AutoSize = value; }
        }

        [Browsable(false)]
        public override bool AllowDrop
        {
            get { return base.AllowDrop; }
            set { base.AllowDrop = value; }
        }

        [Browsable(false)]
        public override bool AutoScroll
        {
            get { return base.AutoScroll; }
            set { base.AutoScroll = value; }
        }

        [Browsable(false)]
        public override System.Drawing.Image BackgroundImage
        {
            get { return base.BackgroundImage; }
            set { base.BackgroundImage = value; }
        }

        [Browsable(false)]
        public override System.Windows.Forms.ImageLayout BackgroundImageLayout
        {
            get { return base.BackgroundImageLayout; }
            set { base.BackgroundImageLayout = value; }
        }
        #endregion
    }
}

